﻿using System;
using System.Text;
using System.Web.Mvc;
using Wedn.Net.Utility;

namespace Wedn.Net.Extensions
{
    /// <summary>
    /// 分页PagerHelper
    /// </summary>
    public static class PagerHelper
    {
        #region 生成分页HTML代码 +static MvcHtmlString Pager(this UrlHelper url, string action, string controller, int totalCount, int current = 1, int pageSize = 10, int showCount = 9)
        /// <summary>
        /// 生成分页HTML代码
        /// </summary>
        /// <param name="url"></param>
        /// <param name="action"></param>
        /// <param name="controller"></param>
        /// <param name="totalCount"></param>
        /// <param name="current"></param>
        /// <param name="pageSize"></param>
        /// <param name="showCount"></param>
        /// <returns></returns>
        public static MvcHtmlString Pager(this UrlHelper url, string action, string controller, int totalCount, int current = 1, int pageSize = 10, int showCount = 9)
        {
            var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数
            var region = (int)Math.Floor(showCount / 2.0);
            var beginNum = current - region <= 0 ? 1 : current - region;
            var endNum = beginNum + showCount;
            if (endNum > totalPages)
            {
                endNum = totalPages + 1;
                beginNum = endNum - showCount;
                beginNum = beginNum < 1 ? 1 : beginNum;
            }
            var pager = new StringBuilder("<ul class=\"pager\">\r\n");
            if (current != 1)
                pager.AppendFormat("\t<li><a href='{0}'>{1}</a></li>\r\n", url.Action(action, controller, new { page = current - 1, q = RequestHelper.QueryString("q") }), "上一页");
            if (beginNum != 1)
                pager.Append("\t<li><span>&hellip;</span></li>\r\n");
            for (var i = beginNum; i < endNum; i++)
            {
                if (i != current)
                    pager.AppendFormat("\t<li><a href='{0}'>{1}</a></li>\r\n", url.Action(action, controller, new { page = i, q = RequestHelper.QueryString("q") }), i);
                else
                    pager.AppendFormat("\t<li class=\"active\"><span>{0}</span></li>\r\n", current);
            }
            if (endNum != totalPages + 1)
                pager.Append("\t<li><span>&hellip;</span></li>\r\n");
            if (current != totalPages)
                pager.AppendFormat("\t<li><a href='{0}'>{1}</a></li>\r\n", url.Action(action, controller, new { page = current + 1, q = RequestHelper.QueryString("q") }), "下一页");
            pager.Append("</ul>");
            return new MvcHtmlString(pager.ToString());
        }
        #endregion
        #region 生成分页HTML代码 +static string GetPager(string format, int totalPages, int current = 1, int showCount = 9)
        /// <summary>
        /// 生成分页HTML代码
        /// </summary>
        /// <remarks>
        /// 2013年10月30日 18:02:07 创建方法 汪磊
        /// </remarks>
        /// <param name="format">当前URL格式其中分页位置用@代替</param>
        /// <param name="totalPages">总页数</param>
        /// <param name="current">当前页码</param>
        /// <param name="showCount">显示几个页码</param>
        /// <example>
        /// <code>
        ///     <div>@BPager.GetPager("http://www.wedn.net/479-489-497-0-0-0-0-0-0-0-0-6-@-0-0-0-0-0-0/",10,1,11)</div>
        /// </code>
        /// </example>
        /// <returns>分页HTML代码</returns>
        public static string GetPager(string format, int totalPages, int current = 1, int showCount = 9)
        {
            var prevAndNext = format.Split('@');
            //var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数
            var region = (int)Math.Floor(showCount / 2.0);
            var beginNum = current - region <= 0 ? 1 : current - region;
            var endNum = beginNum + showCount;
            if (endNum > totalPages)
            {
                endNum = totalPages + 1;
                beginNum = endNum - showCount;
                beginNum = beginNum < 1 ? 1 : beginNum;
            }
            var pager = new StringBuilder("<ul>\r\n");
            if (current != 1)
                pager.AppendFormat("\t<li><a href=\"{1}{0}{2}\">上一页</a></li>\r\n", current - 1, prevAndNext[0], prevAndNext[1]);
            if (beginNum != 1)
                pager.Append("\t<li><span>&hellip;</span></li>\r\n");
            for (var i = beginNum; i < endNum; i++)
            {
                if (i != current)
                    pager.AppendFormat("\t<li><a href=\"{1}{0}{2}\">{0}</a></li>\r\n", i, prevAndNext[0], prevAndNext[1]);
                else
                    pager.AppendFormat("\t<li class=\"active\"><span>{0}</span></li>\r\n", current);
            }
            if (endNum != totalPages + 1)
                pager.Append("\t<li><span>&hellip;</span></li>\r\n");
            if (current != totalPages)
                pager.AppendFormat("\t<li><a href=\"{1}{0}{2}\">下一页</a></li>\r\n", current + 1, prevAndNext[0], prevAndNext[1]);
            pager.Append("</ul>");
            return pager.ToString();
        } 
        #endregion

    }
}